package csao.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import csao.ui.DayPanel;

public class CSAzubiOrganizer extends JPanel {

	private GregorianCalendar gcal = null;

	private GridBagConstraints gbcMain = null;
	private int weekOfYear = 0;

	private JFrame frame = null;
	private JButton prevWeek = null;
	private JButton nextWeek = null;
	private JLabel weekOfYearPanel = null;
	private DayPanel dayPanel[] = null;

	public static void main(String[] args) {
		new CSAzubiOrganizer();
	}

	public CSAzubiOrganizer() {
		gcal = new GregorianCalendar();
		gbcMain = new GridBagConstraints();

		frame = new JFrame("Azubi Organisierer!");
		dayPanel = new DayPanel[7];

		Date today = new Date();
		System.out.println("Today: " + today);
		gcal.setTime(today);

		init();
		addComponents();

		loadWeek(gcal.get(Calendar.WEEK_OF_YEAR));

		display();
	}

	private void init() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridBagLayout());

		setLayout(new GridBagLayout());
		setPreferredSize(new Dimension(800, 600));
	}

	private void loadWeek(int week) {
		System.out.println("Loading KW " + week);
		gcal.set(Calendar.WEEK_OF_YEAR, week);
		int dayOfWeek = gcal.get(Calendar.DAY_OF_WEEK);

		// get to beginning of week
		while (dayOfWeek > Calendar.MONDAY) {
			gcal.add(Calendar.DAY_OF_WEEK, -1);
			dayOfWeek = gcal.get(Calendar.DAY_OF_WEEK);
			// System.out.println(dayOfWeek);
		}

		// get calendar week
		weekOfYear = gcal.get(Calendar.WEEK_OF_YEAR);
		weekOfYearPanel.setText("KW " + weekOfYear);

		for (int i = 0; i < 7; i++) {
			// System.out.println(dayOfWeek);
			dayPanel[i].setDate(gcal.getTime());
			// System.out.println(gcal.getTime());
			gcal.add(Calendar.DAY_OF_WEEK, 1);
			dayOfWeek = gcal.get(Calendar.DAY_OF_WEEK);
		}
	}

	private void addComponents() {
		addMenu();
		addHead();
		addPanels();
	}

	private void addMenu() {
		// Create the menu bar.
		JMenuBar menuBar = new JMenuBar();

		// Build the first menu.
		JMenu menu = new JMenu("A Menu");
		menu.setMnemonic(KeyEvent.VK_A);
		menu.getAccessibleContext().setAccessibleDescription(
				"The only menu in this program that has menu items");
		menuBar.add(menu);

		// a group of JMenuItems
		JMenuItem menuItem = new JMenuItem("A text-only menu item",
				KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This doesn't really do anything");
		menu.add(menuItem);

		frame.setJMenuBar(menuBar);
	}

	private void addHead() {
		JPanel headPanel = new JPanel();
		headPanel.setBackground(Color.yellow);
		headPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbcHead = new GridBagConstraints();

		prevWeek = new JButton("<");
		prevWeek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				loadWeek(weekOfYear - 1);
			}
		});
		gbcHead.gridx = gbcHead.gridy = 0;
		// gbcHead.gridwidth = gbcHead.gridheight = 1;
		gbcHead.anchor = GridBagConstraints.NORTHWEST;
		headPanel.add(prevWeek, gbcHead);

		weekOfYearPanel = new JLabel("KW " + weekOfYear);
		gbcHead.gridx++;
		gbcHead.gridwidth = 2;
		gbcHead.anchor = GridBagConstraints.CENTER;
		gbcHead.fill = GridBagConstraints.HORIZONTAL;
		headPanel.add(weekOfYearPanel, gbcHead);

		nextWeek = new JButton(">");
		nextWeek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				loadWeek(weekOfYear + 1);
			}
		});
		gbcHead.gridx += gbcHead.gridwidth;
		gbcHead.gridwidth = 1;
		gbcHead.anchor = GridBagConstraints.NORTHEAST;
		gbcHead.fill = GridBagConstraints.NONE;
		headPanel.add(nextWeek, gbcHead);

		gbcMain.gridx = gbcMain.gridy = 0;
		gbcMain.gridwidth = gbcMain.gridheight = 1;
//		gbcMain.fill = GridBagConstraints.BOTH;
		add(headPanel, gbcMain);
	}

	private void addPanels() {
		JPanel panelPanel = new JPanel();
		panelPanel.setBackground(Color.cyan);
		panelPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbcPanel = new GridBagConstraints();

		gbcPanel.gridx = gbcPanel.gridy = 0;
		for (int i = 0; i < 7; i++) {
			dayPanel[i] = new DayPanel();
			add(dayPanel[i], gbcPanel);
			gbcPanel.gridy++;
		}

		gbcMain.gridx++;
		gbcMain.gridy++;
		System.out.println(gbcMain.gridy);
		add(panelPanel, gbcMain);
	}

	private void display() {
		frame.add(this);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}
